<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>子访问父</title>
  </head>
  <body>
    <div id="app">
      <cpn> </cpn>
    </div>
    <template id="cpn">
      <div>
        <h3>我是子组件</h3>
        <button @click="btnClick">按钮</button>
      </div>
    </template>
    <script src="../vue.js"></script>
    <template id="cpn">
      <div>
        <ccpn></ccpn>
      </div>
    </template>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          message: "hello",
        },
        components: {
          cpn: {
            template: "#cpn",
            data() {
              return {
                name: "我是cpn组件的name",
              };
            },
            methods: {
              btnClick() {
                //1.访问父组件
                // console.log(this.$parent);
                // console.log(this.$parent.name);
                // //2.访问跟组件$root
                console.log(this.$root);
                console.log(this.$root.message);
              },
            },
            components: {
              ccpn: {
                template: "",
                methods: {
                  btnClick() {
                    console.log(this.$parent);
                  },
                },
              },
            },
          },
        },
      });
    </script>
  </body>
</html>
